home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / conqsrc.lha / Conquest / src / config.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-22  |  2.2 KB  |  115 lines

  1. /* Config.c - configuration file handler */
  2.  
  3. #include <stdio.h>
  4. #include "defs.h"
  5. #include "structs.h"
  6.  
  7.  
  8. /* What things are to be found in the config file? */
  9. #define CONFIG_NOP
  10. #define CONFIG_ERROR     -2
  11. #define CONFIG_END       -1
  12.  
  13. struct config conf =
  14. { FALSE, 0, MAX_BOARD_SIZE, MAX_BOARD_SIZE, MAX_NUM_STARS };
  15.  
  16. char *config_handles[] =
  17. {
  18. "ordering",
  19. "difficulty",
  20. "width",
  21. "height",
  22. "stars",
  23. NULL
  24. };
  25.  
  26. char *next_config_token(FILE *f)
  27. {
  28.   static char buf[80];
  29.   char *b;
  30.   
  31.   for (;;)
  32.   {
  33.     if (fscanf(f, "%s",buf)!=1)
  34.       return(NULL);
  35.     
  36.     switch (*buf)
  37.     {
  38.      case '#':
  39.      case '\n':
  40.      case 0: break;
  41.      default: 
  42.       for (b=b; (*b = tolower(*b)); b++);
  43.       return(buf);
  44.     }
  45.   }
  46. }
  47.  
  48. uint get_config_value(char *text)
  49. {
  50.   if (!text)
  51.     return(0);
  52.  
  53.   return(atol(text));
  54. }
  55.  
  56. int get_config_handle(char *text)
  57. {
  58.   int i;
  59.  
  60.   if (!text)
  61.     return(CONFIG_END);
  62.  
  63.   for (i = 0; config_handles[i]; i++)
  64.     if (!strncmp(config_handles[i], text, strlen(config_handles[i])))
  65.       break;
  66.  
  67.   if (config_handles[i])
  68.     return(i);
  69.  
  70.   return(CONFIG_ERROR);
  71. }
  72.  
  73. bool read_config(char *filename)
  74. {
  75.   FILE *cf;
  76.   int i;
  77.  
  78.   if (cf != fopen(filename,"r"))
  79.     return(FALSE);
  80.  
  81.   for (;;)
  82.   {
  83.     i = get_config_handle(next_config_token(cf));
  84.     switch (i)
  85.     {
  86.      case CONFIG_END: /* Normal termination of configfile */
  87.       return(TRUE);
  88.      case CONFIG_ERROR: /* Abnormal termination, e.g. by bad keyword */
  89.       return(FALSE);
  90.      case 0: /* Stars are ordered */
  91.       conf.stars_ordered = TRUE;
  92.       break;
  93.      case 1: /* Difficulty */
  94.       conf.difficulty = get_config_value(next_config_token(cf));
  95.       break;
  96.      case 2:
  97.       conf.board_width = get_config_value(next_config_token(cf));
  98.       if (conf.board_width > MAX_BOARD_SIZE)
  99.     conf.board_width = MAX_BOARD_SIZE;
  100.       break;
  101.      case 3:
  102.       conf.board_height = get_config_value(next_config_token(cf));
  103.       if (conf.board_height > MAX_BOARD_SIZE)
  104.     conf.board_height = MAX_BOARD_SIZE;
  105.       break;
  106.      case 4:
  107.       conf.num_stars = get_config_value(next_config_token(cf));
  108.       if (conf.num_stars > MAX_NUM_STARS)
  109.     conf.num_stars = MAX_NUM_STARS;
  110.      default:
  111.       fprintf(stderr, "Unimplemented configuration #%d.\n",i);
  112.     }
  113.   }
  114. }
  115.